from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

# 假设你已经有了鸢尾花数据集 X 和 y
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 使用KNN回归
knn_reg = KNeighborsRegressor(n_neighbors=5)
knn_reg.fit(X_train, y_train)  # 训练模型
y_pred_reg = knn_reg.predict(X_test)  # 预测花瓣长度
mse = mean_squared_error(y_test, y_pred_reg)  # 计算均方误差
print("Mean Squared Error:", mse)